package com.loe.ui.page

import android.os.Bundle
import android.view.Gravity
import android.webkit.JavascriptInterface
import com.loe.mvp.BaseActivity
import com.loe.mvp.ext_app.*
import com.loe.mvp.ext_java.AsyncTimer
import com.loe.mvp.ext_java.safe
import com.loe.mvp.ext_java.timer
import com.loe.mvp.ext_view.visible
import com.loe.ui.*
import com.loe.ui.util.SParam
import com.quickjs.JSObject
import kotlinx.android.synthetic.main.activity_ui.*

class UiActivity : BaseActivity()
{
    private var loop: AsyncTimer? = null

    override val path get() = jsClass

    private val url by lazy { intent.getStringExtra("url") ?: "" }

    private lateinit var jsClass: String

    private lateinit var params: JSObject

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ui)

        val urlParam = SParam(url.replace("loe://", ""))
        jsClass = urlParam.path
        params = JSObject(jsContext, urlParam.paramJson())

        viewConsole.visible = LoeUI.isPreview

        // 加载过了
        if (UiSpMain.md5.isNotEmpty())
        {
            init()
        } else
        {
            // 加载
            LoeUI.loadPackJs()
            {
                init()
            }
        }

        initEvent()
    }

    /** 初始化page */
    fun init()
    {
        uiPage.init(jsClass)
        {
            val page = this
            it.set("params", params)
            it.appendJavascriptInterface(object
            {
                @JavascriptInterface
                fun setTitle(ui: JSObject?)
                {
                    page.setTitle(ui)
                }

                @JavascriptInterface
                fun toast(msg: String?)
                {
                    page.toast(msg)
                }

                @JavascriptInterface
                fun showLoading(msg: String?)
                {
                    page.showLoading(msg ?: "")
                }

                @JavascriptInterface
                fun cancelLoading()
                {
                    page.cancelLoading()
                }

                @JavascriptInterface
                fun finish()
                {
                    page.onBackPressed()
                }

                @JavascriptInterface
                fun openUrl(urlString: String)
                {
                    page.openUrl(urlString)
                }

                @JavascriptInterface
                fun openDialog(s: String)
                {
                    UiDialog(activity, s).show()
                }

                @JavascriptInterface
                fun hideSoft()
                {
                    page.hideSoft()
                }

                @JavascriptInterface
                fun transIn(type: String)
                {
                    when (type)
                    {
                        "scale" -> page.transInScale()
                        "alpha" -> page.transInAlpha()
                        "bottom" -> page.transInBottom()
                        "top" -> page.transInTop()
                        else -> page.overridePendingTransition(R.anim.slide_right_in, R.anim.on)
                    }
                }

                @JavascriptInterface
                fun transOut(type: String)
                {
                    when (type)
                    {
                        "scale" -> page.transOutScale()
                        "bottom" -> page.transOutBottom()
                        "top" -> page.transOutTop()
                        else -> page.overridePendingTransition(R.anim.on, R.anim.slide_right_out)
                    }
                }
            })
        }
    }

    override fun onBus(msg: String)
    {
        safe { uiPage.runJsFun("onBus", msg) }
    }

    override fun onResume()
    {
        super.onResume()
        safe { uiPage.runJsFun("onResume") }
    }

    override fun onPause()
    {
        super.onPause()
        if (loop != null) buttonLoop.performClick()
        safe { uiPage.runJsFun("onPause") }
    }

    override fun onDestroy()
    {
        safe { uiPage.runJsFun("onDestroy") }

        loop?.finish()
        super.onDestroy()
    }

    /**
     * 设置标题
     */
    private fun setTitle(titleUI: JSObject?)
    {
        if (titleUI == null)
        {
            titleView.visible = false
            viewShadow.visible = false
            return
        }

        titleUI.gotHasBoolean("visible")
        {
            titleView.visible = it
            viewShadow.visible = it
        }

        titleUI.gotHasBoolean("shadowVisible") { viewShadow.visible = it }

        val statusLight = titleUI.gotBoolean("statusLight")

        if (titleUI.contains("full"))
        {
            titleView.setStatus(statusLight, titleUI.gotBoolean("full"))
        }
        titleUI.gotHasString("statusBg") { setStatusBar(it.color, !statusLight) }
        titleUI.gotHasBoolean("leftVisible") { titleView.setLeftVisible(it) }
        titleUI.gotHasString("leftImage") { image -> getUrlDrawable(image) { titleView.setLeftImageDrawable(it) } }
        titleUI.gotHasString("barHeight") { titleView.setBarHeight(it.px) }
        titleUI.gotHasBoolean("barVisible") { titleView.setBarVisible(it) }
        titleUI.gotHas("barBg")
        {
            if (it is JSObject)
            {
                titleView.bar.background = it.shape
            } else
            {
                titleView.bar.bg = it.toString()
            }
        }
        titleUI.gotHasString("title") { titleView.setTitle(it) }
        titleUI.gotHasBoolean("titleBold") { titleView.setTitleBold(it) }
        titleUI.gotHasString("titleSize") { titleView.setTitleSize(it.pxFloat) }
        titleUI.gotHasString("titleGravity")
        {
            titleView.setTitleGravity(
                when (it)
                {
                    "l" -> Gravity.LEFT
                    "r" -> Gravity.RIGHT
                    else -> Gravity.CENTER
                }
            )
        }
        titleUI.gotHasString("right") { titleView.setRight(it) }
        titleUI.gotHasString("rightWidth") { titleView.setRightWidth(it.pxFloat) }
        titleUI.gotHasString("rightImage") { image -> getUrlDrawable(image) { titleView.setRightImageDrawable(it) } }
        titleUI.gotHasString("color") { titleView.setColor(it.color) }

        titleUI.gotHas("bg")
        {
            if (it is JSObject)
            {
                titleView.background = it.shape
            } else
            {
                titleView.bg = it.toString()
            }
        }
    }

    /******************************************* test *************************************/

    override fun loadData()
    {
        LoeUI.loadPackJs()
        {
            init()
        }
    }

    override fun initEvent()
    {
        buttonRefresh.setOnClickListener()
        {
            loadData()
        }

        buttonLoop.setOnClickListener()
        {
            if (loop != null)
            {
                loop?.finish()
                loop = null
                buttonLoop.setTextColor(color(R.color.textColor))
            } else
            {
                loop = timer(0, 400)
                {
                    LoeUI.loadPackJs()
                    {
                        if (it) init()
                    }
                }
                buttonLoop.setTextColor(color(R.color.greenDeep))
            }
        }
        if (LoeUI.isPreview) buttonLoop.performClick()

        buttonOpen.setOnClickListener()
        {
            start(UiActivity::class).putExtra("url", url)
        }
    }
}